import axios from 'axios'
// import { MessageBox, Message } from 'element-ui'
import store from '@/store'
console.log('当前的store信息是', store)
// import { getToken } from '@/utils/auth'

// create an axios instance
const service = axios.create({
//   baseURL: 'http://ihrm-java.itheima.net/api', // 设置axios请求的基础的基础地址
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})
// request interceptor
// 请求拦截器
service.interceptors.request.use(
  config => {
    // do something before request is sent
    // 如果当前存有token 就加在请求头上
    const token = store.getters.token
    if (token) {
      // let each request carry token
      // ['Authorization'] is a custom headers key
      // please modify it according to the actual situation
      config.headers['Authorization'] = `Bearer ${token}`
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
// 响应拦截器
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
  */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    //  与后盾接口约定 登陆成功后返回的值是什么
    console.log('响应的结果是', response)
    const res = response.data
    if (res.success) {
      return res
    } else {
      // Promise.reject()方法返回一个带有拒绝原因的Promise对象。
      // 待定（pending）: 初始状态，既没有被兑现，也没有被拒绝。
      // 已兑现（fulfilled）: 意味着操作成功完成。
      // 已拒绝（rejected）: 意味着操作失败。
      return Promise.reject(new Error(res.message))
    }
  }
  //   error => {
  //     console.log('err' + error) // for debug
  //     Message({
  //       message: error.message,
  //       type: 'error',
  //       duration: 5 * 1000
  //     })
  //     return Promise.reject(error)
  //   }

)

export default service
